<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>
    <div th:fragment="onLoadJS">
        <script th:src="@{/js/debug/gtdebug.0.0.2.js}" apikey="s68tU4HgPAXYyErnotPNGuiIPwYRxMS7VuxPErsN8J3SsRICxTzVYIEu0h1TBTfW"></script>
        <script th:src="@{/js/login/jquery.min.js}"></script>
        <!--雪花js -->
        <!-- <script th:src="@{/js/login/snow.js}"></script> -->
        <script th:src="@{/js/login/jquery.pure.tooltips.js}"></script>
        <script th:src="@{/js/login/spop.min.js}"></script>
        <script th:src="@{/js/login/jquery.cookie.js}"></script>
        <script th:src="@{/js/common/request.js}"></script>
        <script th:src="@{/js/layer/layer.js}"></script>
        <script>
            $(function () {
                console.log("userName=",$("#reset-username").val());
                if($("#reset-username").val() != "1"){
                    goto_resetpassword();
                }
            });
            (function() {
                // trim polyfill : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim
                if (!String.prototype.trim) {
                    (function() {
                        // Make sure we trim BOM and NBSP
                        var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
                        String.prototype.trim = function() {
                            return this.replace(rtrim, '');
                        };
                    })();
                }

                [].slice.call( document.querySelectorAll( 'input.input__field' ) ).forEach( function( inputEl ) {
                    // in case the input is already filled..
                    if( inputEl.value.trim() !== '' ) {
                        classie.add( inputEl.parentNode, 'input--filled' );
                    }

                    // events:
                    inputEl.addEventListener( 'focus', onInputFocus );
                    inputEl.addEventListener( 'blur', onInputBlur );
                } );

                function onInputFocus( ev ) {
                    classie.add( ev.target.parentNode, 'input--filled' );
                }

                function onInputBlur( ev ) {
                    if( ev.target.value.trim() === '' ) {
                        classie.remove( ev.target.parentNode, 'input--filled' );
                    }
                }
            })();

            $(function() {
                $('#login #login-password').focus(function() {
                    $('.login-owl').addClass('password');
                }).blur(function() {
                    $('.login-owl').removeClass('password');
                });
                $('#login #register-password').focus(function() {
                    $('.register-owl').addClass('password');
                }).blur(function() {
                    $('.register-owl').removeClass('password');
                });
                $('#login #register-repassword').focus(function() {
                    $('.register-owl').addClass('password');
                }).blur(function() {
                    $('.register-owl').removeClass('password');
                });
                $('#login #forget-password').focus(function() {
                    $('.forget-owl').addClass('password');
                }).blur(function() {
                    $('.forget-owl').removeClass('password');
                });
            });


            function goto_register(){
                $("#register-username").val("");
                $("#register-password").val("");
                $("#register-teamname").val("");
                $("#register-nickname").val("");
                $("#register-captcha").val("");
                $("#reset-div").hide();
                $("#tab-2").prop("checked",true);
            }

            function goto_login(){
                $("#login-username").val("");
                $("#login-password").val("");
                $("#reset-div").hide();
                $("#tab-1").prop("checked",true);
            }

            function goto_forget(){
                $("#forget-username").val("");
                $("#reset-div").hide();
                $("#tab-3").prop("checked",true);
            }

            function goto_resetpassword() {

                $("#reset-password").val("");
                $("#reset-div").show();
                $("#tab-1").prop("checked",false);
                $("#tab-2").prop("checked",false);
                $("#tab-3").prop("checked",false);
            }
            //重置密码
            function resetPwd() {
                var password = $("#reset-password").val(),
                    username = $("#reset-username").val(),
                    flag = false;
                if(password == ""){
                    $.pt({
                        target: $("#reset-password"),
                        position: 'r',
                        align: 't',
                        width: 'auto',
                        height: 'auto',
                        content:"密码不能为空"
                    });
                    flag = true;
                }else{
                    if(password.length < 6){
                        $.pt({
                            target: $("#reset-password"),
                            position: 'r',
                            align: 't',
                            width: 'auto',
                            height: 'auto',
                            content:"密码不能小于6位"
                        });
                        flag = true;
                    }
                }
                console.log(flag)
                if(flag){
                    return false;
                }else{
                    send(
                        '/sys/resetPassword',
                        {userName: username, password: password},
                        function (data) {
                            if(data.result == 0){
                                spop({
                                    template: '<h4 class="spop-title">重置成功</h4>即将于3秒后返回登录',
                                    position: 'top-center',
                                    style: 'success',
                                    autoclose: 3000,
                                    onOpen : function(){
                                        var second = 2;
                                        var showPop = setInterval(function(){
                                            if(second == 0){
                                                clearInterval(showPop);
                                            }
                                            $('.spop-body').html('<h4 class="spop-title">重置成功</h4>即将于'+second+'秒后返回登录');
                                            second--;
                                        },1000);
                                    },
                                    onClose : function(){
                                        goto_login();
                                    }
                                });
                            }else{
                                layer.msg(data.message);
                            }
                        },
                        function (res) {
                            
                        }
                    );
                }
            }

//            $("body").keydown(function() {
//                if (event.keyCode == "13") {//keyCode=13是回车键
//
//                }
//            });

            function login(){//登录
                //window.location.href="/sys/index";
                var username = $("#login-username").val(),
                    password = $("#login-password").val(),
                    validatecode = null,
                    flag = false;
                //判断用户名密码是否为空
                if(username == ""){
                    $.pt({
                        target: $("#login-username"),
                        position: 'r',
                        align: 't',
                        width: 'auto',
                        height: 'auto',
                        content:"用户名不能为空"
                    });
                    flag = true;
                }
                var regExp = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
                if(!regExp.test(username)){
                    $.pt({
                        target: $("#login-username"),
                        position: 'r',
                        align: 't',
                        width: 'auto',
                        height: 'auto',
                        content:"邮箱格式错误"
                    });
                    flag = true;
                }
                if(password == ""){
                    $.pt({
                        target: $("#login-password"),
                        position: 'r',
                        align: 't',
                        width: 'auto',
                        height: 'auto',
                        content:"密码不能为空"
                    });
                    flag = true;
                }

                if(flag){
                    return false;
                }else{//登录
                    //调用后台登录验证的方法
                    send(
                        '/getToken',
                        {username: username, password: password},
                        function (data) {
                            if(data.code == "200"){
                                var token = data.data.access_token;
                                window.location.href='/sys/index?access_token='+token;
                                $.cookie("access_token",token,{ expires: 365, path: '/' });
                            }else{
                                layer.msg(data.message);
                                return false;
                            }
                        },
                        function (res) {
                            layer.msg("网络异常，请稍后再试！");
                        }
                    );
                    return false;
                }
            }

            //注册
            function register(){
                var username = $("#register-username").val(),
                    password = $("#register-password").val(),
                    teamname = $("#register-teamname").val(),
                    nickname = $("#register-nickname").val(),
                    captcha = $("#register-captcha").val(),
                    flag = false;
                //判断用户名密码是否为空
                if(username == ""){
                    $.pt({
                        target: $("#register-username"),
                        position: 'r',
                        align: 't',
                        width: 'auto',
                        height: 'auto',
                        content:"邮箱不能为空"
                    });
                    flag = true;
                }
                var regExp = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
                if(!regExp.test(username)){
                    $.pt({
                        target: $("#register-username"),
                        position: 'r',
                        align: 't',
                        width: 'auto',
                        height: 'auto',
                        content:"邮箱格式错误"
                    });
                    flag = true;
                }
                if(password == ""){
                    $.pt({
                        target: $("#register-password"),
                        position: 'r',
                        align: 't',
                        width: 'auto',
                        height: 'auto',
                        content:"密码不能为空"
                    });
                    flag = true;
                }
                if(teamname == ""){
                    $.pt({
                        target: $("#register-teamname"),
                        position: 'r',
                        align: 't',
                        width: 'auto',
                        height: 'auto',
                        content:"团队名称不能为空"
                    });
                    flag = true;
                }
                if(nickname == ""){
                    $.pt({
                        target: $("#register-nickname"),
                        position: 'r',
                        align: 't',
                        width: 'auto',
                        height: 'auto',
                        content:"用户名不能为空"
                    });
                    flag = true;
                }
                if(captcha == ""){
                    $.pt({
                        target: $("#register-captcha"),
                        position: 'r',
                        align: 't',
                        width: 'auto',
                        height: 'auto',
                        content:"验证码不能为空"
                    });
                    flag = true;
                }
                if(flag){
                    return false;
                }else{//注册
                    send(
                        '/sys/register',
                        {userName: username,password: password,teamName: teamname,nickName: nickname, captcha: captcha},
                        function (data) {
                            if(data.result == 0){
                                spop({
                                    template: '<h4 class="spop-title">注册成功</h4>即将于3秒后返回登录',
                                    position: 'top-center',
                                    style: 'success',
                                    autoclose: 3000,
                                    onOpen : function(){
                                        var second = 2;
                                        var showPop = setInterval(function(){
                                            if(second == 0){
                                                clearInterval(showPop);
                                            }
                                            $('.spop-body').html('<h4 class="spop-title">注册成功</h4>即将于'+second+'秒后返回登录');
                                            second--;
                                        },1000);
                                    },
                                    onClose : function(){
                                        goto_login();
                                    }
                                });
                            }else{
                                $.pt({
                                    target: $("#register-button"),
                                    position: 'r',
                                    align: 't',
                                    width: 'auto',
                                    height: 'auto',
                                    content:data.message
                                });
                            }
                        },
                        function (res) {
                            $.pt({
                                target: $("#register-button"),
                                position: 'r',
                                align: 't',
                                width: 'auto',
                                height: 'auto',
                                content:'网络异常，请稍后再试！'
                            });
                        }
                    );
                    return false;
                }
            }

            //重置密码
            function forget(){
                var username = $("#forget-username").val(),
                    flag = false;
                //判断用户名密码是否为空
                if(username == ""){
                    $.pt({
                        target: $("#forget-username"),
                        position: 'r',
                        align: 't',
                        width: 'auto',
                        height: 'auto',
                        content:"邮箱不能为空"
                    });
                    flag = true;
                }

                //用户名只能是15位以下的字母或数字
                var regExp = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
                if(!regExp.test(username)){
                    $.pt({
                        target: $("#forget-username"),
                        position: 'r',
                        align: 't',
                        width: 'auto',
                        height: 'auto',
                        content:"邮箱格式错误"
                    });
                    flag = true;
                }
                //检查用户名是否存在
                //调后台方法
                if(flag){
                    return false;
                }else{//重置密码
                    send(
                        '/sys/forget',
                        {userName: username},
                        function (data) {
                            if(data.result == "0"){
                                spop({
                                    template: '邮件发送成功，请去邮箱查看！',
                                    position  : 'top-center',
                                    style: 'success',
                                    autoclose: 3000
                                });
                            }else{
                                spop({
                                    template: data.message,
                                    position  : 'top-center',
                                    style: 'error'
                                });
                            }
                        },
                        function (res) {
                            spop({
                                template: '网络异常，请稍后再试！',
                                position  : 'top-center',
                                style: 'error'
                            });
                        }
                    );
                    return false;
                }
            }

            //看不清，换一张
            function changeImg() {
                $('#captchaImg').attr('src', '/sys/captcha?date=' + new Date());
            }
        </script>
    </div>
</body>
</html>